<template>
  <div class="container">
    <div v-if="is_show"
         class="entry_img">
      <img mode="widthFix"
           :src="img_base+entry_img_url"
           v-if="img_base&&entry_img_url"
           class="img">
      <button class="btn-login"
              :open-type="btn_type"
              @getuserinfo="get_info"
              @opensetting="get_setting"></button>
    </div>
    <div v-if="!is_show"
         class="tel_wrap">
      <div class="tel_img">
        <div class="title">微信授权</div>
        <img src="https://h5.ameimeika.com/mp_images/mp_2.4/duigou@2x.png"
             class="img">
        <div class="success">微信授权成功</div>
        <div class="get_num">授权绑定你的手机号</div>
      </div>
      <div class="bottom">
        <button @tap="reject"
                class="btn reject">拒绝</button>
        <button open-type="getPhoneNumber"
                class="btn sure"
                @getphonenumber="getnum">允许</button>
      </div>
    </div>
  </div>
</template>

<script>
import common from "@/assets/js/mmk_common.js";
export default {
  data () {
    return {
      entry_img_url: "",
      img_base: common.image_response,
      btn_type: "getUserInfo",
      jump_url: "",
      title: "",
      s_data: {},
      is_show: true,
      isReject: false,
      code: "",
      u_id: "",
      login_data: {},
    };
  },
  onLoad (options) {
    Object.assign(this.$data, this.$options.data());
    wx.removeStorage({
      key: 'user_token',
      success: res => { }
    });
    if (options.scene) {
      let url_data = decodeURIComponent(options.scene),
        obj = {};
      url_data.split("&").forEach(item => {
        obj[item.split("=")[0]] = item.split("=")[1];
      });

      if (obj.sid && obj.t && obj.tg) {
        this.s_data = obj;
      } else {
        wx.showToast({
          title: "参数不完整", //提示的内容,
          icon: "none", //图标,
          duration: 1500, //延迟时间,
          mask: true, //显示透明蒙层，防止触摸穿透,
          success: res => {
            setTimeout(() => {
              wx.switchTab({ url: '/pages/index/main' });
            }, 1500);
          }
        });
      }
    } else if (options.t && options.tg) {
      this.s_data = options;
    } else {
      wx.showToast({
        title: "参数不完整", //提示的内容,
        icon: "none", //图标,
        duration: 1500, //延迟时间,
        mask: true, //显示透明蒙层，防止触摸穿透,
        success: res => {
          setTimeout(() => {
            wx.switchTab({ url: '/pages/index/main' });
          }, 1500);
        }
      });
    }
    if (this.title != "") {
      wx.setNavigationBarTitle({
        title: this.title
      });
    }
  },
  onReady () {
    this.get_entryData();
    this.get_logincode()
  },
  onShow () {
    wx.removeStorage({
      key: 'user_token',
      success: res => { }
    });
    this.get_logincode()
  },
  methods: {
    getnum (e) {
      let that = this
      if (e.mp.detail.errMsg == "getPhoneNumber:ok") {
        wx.login({
          success: result => {
            common.fly_post("api/v4_5/minapp/bind_mobile", {
              id: this.u_id, iv: e.mp.detail.iv, code: result.code, encryptedData: e.mp.detail.encryptedData
            }, response => {
              let res = response.data;
              if (res.status_code == 0) {
                wx.setStorageSync("user_token", res.data.token);
                wx.setStorageSync("user_id", res.data.user_id);
                wx.setStorageSync("account_type", res.data.user.data.account_type);
                wx.setStorageSync("role_id", res.data.user.data.role_id);
                that.band_user()
              } else {
                wx.showToast({
                  title: res.message, //提示的内容,
                  icon: "none", //图标,
                  duration: 1500, //延迟时间,
                  mask: false //显示透明蒙层，防止触摸穿透,
                });
              }
            });
          }
        });
      } else {
        let str = encodeURIComponent(this.jump_url)
        wx.redirectTo({
          url: `/pages/package_mine/bind_tel/main?j_url=${str}&user_data=${JSON.stringify(this.s_data)}`
        });
      }
    },
    get_logincode () {
      wx.login({
        success: res => {
          this.login_data = res
        },
        fail: () => {
          wx.showToast({
            title: '登录失败', //提示的内容,
            icon: 'none', //图标,
            duration: 1500, //延迟时间,
            mask: true, //显示透明蒙层，防止触摸穿透,
            success: res => { }
          });
        },
        complete: () => { }
      });
    },
    reject () {
      //不允许,跳转到绑定手机号页面
      let str = encodeURIComponent(this.jump_url)
      wx.redirectTo({
        url: `/pages/package_mine/bind_tel/main?j_url=${str}&user_data=${JSON.stringify(this.s_data)}`
      });
    },
    get_info (e) {
      let that = this;
      wx.getStorage({
        key: 'user_token',
        success: () => {
          // 已登陆
          // 判断有没有绑定手机号  没有就去绑定
          that.band_user()
        },
        fail: () => {
          // 未登录

          common.fly_post(
            "api/v4_5/minapp/login",
            {
              code: that.login_data.code,
              iv: e.mp.detail.iv,
              encryptedData: e.mp.detail.encryptedData,
              avatarUrl: e.mp.detail.userInfo.avatarUrl,
              nickName: e.mp.detail.userInfo.nickName,
              sid: that.s_data.sid,
              source: that.s_data.d
            },
            response => {
              let res = response.data;
              if (res.status_code == 0) {
                that.u_id = res.data.user_id;
                wx.setStorageSync("user_token", res.data.token);
                wx.setStorageSync("user_id", res.data.user_id);
                wx.setStorageSync("open_id", res.data.user_jwt);
                console.log(12355, res.data.user.data.role_id)
                wx.setStorageSync("role_id", res.data.user.data.role_id);
                console.log(444444, wx.getStorageSync("role_id"))
                //绑定手机号弹出获取手机号弹框
                if (res.data.user_status == 0) {
                  // 未绑定手机号
                  that.is_show = false;
                } else {
                  that.band_user()
                  wx.reLaunch({ url: that.jump_url });
                }
                this.get_logincode()
              } else {
                this.get_logincode()
                wx.showToast({
                  title: res.message,
                  icon: "none",
                  duration: 2000,
                  mask: false
                });
              }
            }
          );
        },
        complete: () => { }
      })
    },
    get_setting (e) {
      let that = this;
      wx.getSetting({
        success: res => {
          if (res.authSetting["scope.userInfo"] === true) {
            // 已经授权，不会弹框
            that.btn_type = "getUserInfo";
            // 判断登录
            if (wx.getStorageSync("user_token")) {
              // 已登陆
            } else {
              // 未登陆
              that.btn_type = "getUserInfo";
            }
          } else if (res.authSetting["scope.userInfo"] === false) {
            // 授权弹窗且被拒绝
            wx.showToast({
              title: "您未开启权限", //提示的内容,
              icon: "none", //图标,
              duration: 1500, //延迟时间,
              mask: true, //显示透明蒙层，防止触摸穿透,
              success: res => {
                that.btn_type = "openSetting";
              }
            });
          }
        }
      });
    },
    get_entryData () {
      wx.showLoading({
        title: "加载中", //提示的内容,
        mask: false //显示透明蒙层，防止触摸穿透,
      });
      common.fly_post("api/v4_5/user_jump/url", this.s_data, response => {
        let res = response.data;
        if (res.status_code == 0) {
          wx.hideLoading();
          this.entry_img_url = res.data.img_url;
          this.jump_url = res.data.jump_url;
          this.title = res.data.title;
        } else {
          wx.showToast({
            title: res.message, //提示的内容,
            icon: "none", //图标,
            duration: 1500, //延迟时间,
            mask: false //显示透明蒙层，防止触摸穿透,
          });
          setTimeout(() => {
            wx.switchTab({ url: '/pages/index/main' });
          }, 1500);
        }
      });
    },
    band_user () {
      let that = this
      common.fly_post(
        "api/v4_5/user/share",
        {
          type: that.s_data.t,
          sid: that.s_data.sid || 0,
          d: that.s_data.d
        },
        response => {
          if (that.s_data.t == 13) {
            wx.reLaunch({ url: (that.jump_url + '&share_id=' + that.s_data.sid) });
            console.log(that.jump_url + '&share_id=' + that.s_data.sid);

          } else {
            wx.reLaunch({ url: that.jump_url });
          }

        }
      );
    }
  }
};
</script>

<style lang="less" scoped>
.container {
  background: rgba(0, 0, 0, 0.3);
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .entry_img {
    width: 260px;
    height: auto;
    position: relative;
    .img {
      width: 100%;
      height: 100%;
    }
    .btn-login {
      opacity: 0;
      width: 100%;
      height: 50px;
      position: absolute;
      left: 0;
      bottom: 17px;
    }
  }
  .tel_wrap {
    width: 260px;
    height: 240px;
    text-align: center;
    background: rgba(255, 255, 255, 1);
    border-radius: 8px;
    position: relative;
    bottom: 5%;
    .tel_img {
      text-align: center;
      .img {
        width: 44px;
        height: 44px;
        display: block;
        margin: 0 auto;
        padding: 13px 0;
      }
      .title {
        font-size: 17px;
        font-weight: 900;
        color: rgba(51, 51, 51, 1);
        line-height: 24px;
        padding: 13px 0;
        border-bottom: solid 1px #f2f2f2;
      }
      .success {
        font-size: 14px;
        font-weight: 400;
        color: rgba(51, 51, 51, 1);
        line-height: 20px;
      }
      .get_num {
        margin-top: 13px;
        font-size: 12px;
        color: rgba(153, 153, 153, 1);
        line-height: 17px;
        padding: 13px 0;
        border-bottom: solid 1px #f2f2f2;
        border-top: solid 1px #f2f2f2;
      }
    }
    .bottom {
      width: 100%;
      position: absolute;
      bottom: 0;
      left: 0;
      overflow: hidden;
      .btn {
        float: left;
        width: 50%;
        line-height: 40px;
        font-size: 12px;
        color: #333333;
        background: rgba(255, 255, 255, 1);
        border: none;
      }
      .sure {
        color: #1cab1a;
      }
      .btn::after {
        border: none;
      }
    }
  }
}
</style>


